from django.shortcuts import render
from django.views import View

from apps.contents.models import ContentCategory
from apps.contents.utils import get_categories


class IndexView(View):
    def get(self,request):
        # 1.三级商品分类获取 14句
        categories = get_categories()

        # 2. 广告数据的获取 4句
        # 2.1.获取 所有 广告分类
        ad_categories = ContentCategory.objects.all()
        # 2.2 遍历
        contents = {}
        for ad in ad_categories:
            # 2.3 组合前端需要的数据格式---分类--广告内容 content_set.filter()
            contents[ad.key] = ad.content_set.filter(status=True).order_by('sequence')

        context = {
            'categories': categories,
            'contents': contents
        }
        return render(request, 'index.html', context)
